$(document).ready(function(){
    // render
    loadData();
    // event
    bindEvent();
});

function loadData() {

    ISS.BACKEND.request({
        url: '../../service/admin/getAllCategory',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllCategoryToView(responseContent);
        }
    });

}

function bindEvent() {
    ISS.EVENT.bindEvent('#AdPostCategory_Add1', 'click', function () {
        if(validate()){
            sendInfo();
        }
        else {
            var htmlText = '<div class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Thêm chuyên mục mới thất bại</div>\
                            </div>';
            ISS.DOM.before('.l-register__benefits', htmlText);
        }
    });

    ISS.EVENT.bindEvent('#AdPostCategory_Modified', 'click', function () {
        toEditPage();
    });
}

function sendInfo() {
    var category = {};

    category.maDMBV = ISS.DOM.getVal('#AdPostCategory_code');
    category.tenDM = ISS.DOM.getVal('#AdPostCategory_name');

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/header/adPostCategory',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(category),
        success: function (data) {
            var responseContent = data.responseObject;
            sendInfoComplete(responseContent);
        }
    });
}

function toEditPage() {
    var _maDMBV = [];
    ISS.DOM.forEach('#form-AdPostCategory input[type="checkbox"]:checked', function(){
        _maDMBV.push($(this).val());
    });

    if(_maDMBV.length == 1) {
        var _url = '/service/admin/toPostCategoryPage/' + _maDMBV[0];
        ISS.WINDOW.location(_url);
    }
    else {
        alert('Bạn chưa chọn hoặc đã chọn hơn 1 dòng');
    }
}

function sendInfoComplete(category) {
    if(category) {
        ISS.WINDOW.location('/jsp/admin/ad-post-category-common.jsp');
    }
    else{
        alert('Thêm mới không thành công');
    }
}

function clearValidate() {
    ISS.DOM.removeClass('#AdPostCategory_code', 'error');
    ISS.DOM.removeClass('#AdPostCategory_name', 'error');
}

function validate() {
    var result = true;
    return result;
}

function mapAllCategoryToView(category) {

    var rows = category.length;
    var columns = 1;
    var str = '';
    str += '<table class="scroll align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnSTT">STT</th>';
    str += '       <th class="columnStick">Tick</th>';
    str += '       <th class="columnRule">Mã màu</th>';
    str += '       <th class="columnRule">Tên màu</th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    for(var i = 0; i < rows; i++){
        if (i%2==0)
        {
            str += '<tr class="column-Dark">';
        }
        else
        {
            str += '<tr class="column-Light">';
        }

        for (var j = 0; j < columns; j++){
            str += '<td class="columnSTT">' + i + '</td>';
            str += '<td class="columnStick"><input id="' + category[i].maDMBV + '" type="checkbox" value="' + category[i].maDMBV + '"/></td>';
            str += '<td class="columnRule">' + category[i].maDMBV + '</td>';
            str += '<td class="columnRule">' + category[i].tenDM + '</td>';
        }
        str += '</tr>';
    }
    str +=   '</tbody>';
    str += '</table>';
    ISS.DOM.html('#AdPostCategory_All', str);
}